﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace MiniErp.Common.Https
{
    public class VisitHttps : IVisitHttps
    {
        public const string token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJVSWQiOiIyMTAwMDEiLCJVc2VyTmFtZSI6IuW8oOS4iSIsIkpvYnNJZCI6IjEiLCJleHAiOjE2MjM4NDgzMjUsImlzcyI6Imh0dHA6Ly9sb2NhbGhvc3Q6NTAwMSIsImF1ZCI6Imh0dHA6Ly9sb2NhbGhvc3Q6NTAwMSJ9.4zUI06RRdXMtMUrb40KYtBBWAT1dgwxXu_Kpu-s27qI";

        public string HttpDelete(string url, string token)
        {
            var request = WebRequest.Create(url) as HttpWebRequest;
            request.Method = "DELETE";
            request.ContentType = "application/x-www-form-urlencoded";
            request.Credentials = CredentialCache.DefaultCredentials;
            request.Timeout = 3600;
            request.ReadWriteTimeout = 3600;
            request.Headers["Authorization"] = "Bearer " + token;
            Console.WriteLine($"当前访问URL地址,GET:{url}");
            try
            {
                var myResponse = (HttpWebResponse)request.GetResponse();
                using var reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
                Console.WriteLine($"访问当前URL成功，结果为:");
                return reader.ReadToEnd();
            }
            catch
            {
                return "当前URL访问异常";
            }
        }

        public string HttpGet(string url, string token)
        {
            var request = WebRequest.Create(url) as HttpWebRequest;
            request.Method = "GET";
            request.ContentType = "application/x-www-form-urlencoded";
            request.Credentials = CredentialCache.DefaultCredentials;
            request.Timeout = 3600;
            request.ReadWriteTimeout = 3600;
            request.Headers["Authorization"] = "Bearer " + token;
            Console.WriteLine($"当前访问URL地址,GET:{url}");
            try
            {
                using var sr = new StreamReader(request.GetResponse().GetResponseStream(), Encoding.UTF8);
                Console.WriteLine($"访问当前URL成功，结果为:");
                var result = sr.ReadToEnd();
                return result.Insert(result.Length - 1, "\n").Insert(1, "\n").Replace(",", ",\n");
            }
            catch
            {
                return "当前URL访问异常";
            }
        }


        public string HttpPost(string url, string bodyJson, string token)
        {
            var encoding = Encoding.UTF8;
            var request = WebRequest.Create(url) as HttpWebRequest;
            request.Method = "POST";
            request.ContentType = "application/json;charset=utf-8";
            var buffer = encoding.GetBytes(bodyJson);
            request.Headers["Authorization"] = "Bearer " + token;
            request.ContentLength = buffer.Length;
            using var stream = request.GetRequestStream();
            stream.Write(buffer, 0, buffer.Length);
            Console.WriteLine($"当前访问URL地址,POST:{url}");
            try
            {
                var response = request.GetResponse() as HttpWebResponse;
                using var reader = new StreamReader(response.GetResponseStream(), encoding);
                Console.WriteLine($"访问当前URL成功，结果为:");
                var result = reader.ReadToEnd();
                return result.Insert(result.Length - 1, "\n").Insert(1, "\n").Replace(",", ",\n"); ;
            }
            catch
            {
                return "当前URL访问异常";
            }
        }

        public string HttpPut(string url, string bodyJson, string token)
        {
            var encoding = Encoding.UTF8;
            var request = WebRequest.Create(url) as HttpWebRequest;
            request.Method = "PUT";
            request.ContentType = "application/json;charset=utf-8";
            var buffer = encoding.GetBytes(bodyJson);
            request.Headers["Authorization"] = "Bearer " + token;
            request.ContentLength = buffer.Length;
            using var stream = request.GetRequestStream();
            stream.Write(buffer, 0, buffer.Length);
            Console.WriteLine($"当前访问URL地址,POST:{url}");
            try
            {
                var response = request.GetResponse() as HttpWebResponse;
                using var reader = new StreamReader(response.GetResponseStream(), encoding);
                Console.WriteLine($"访问当前URL成功，结果为:");
                var result = reader.ReadToEnd();
                return result.Insert(result.Length - 1, "\n").Insert(1, "\n").Replace(",", ",\n"); ;
            }
            catch
            {
                return "当前URL访问异常";
            }
        }
    }
}
